import sys
import os
import py7zr
from PyQt5.QtWidgets import QApplication,QMainWindow,QFileDialog,QMessageBox
import SendAndRecv as sar
from uiLib import gui
import shutil


fileNames = ''


def login(ui):
	os.system('pythonw login.py')
	with open('user.info','r')as f:
		sar.user = f.read()
	ui.label_2.setText(sar.user)

def chooseFile(ui):
	global fileNames
	fileNames = QFileDialog.getOpenFileNames(None,'选取要传输文件','./','all file;*.*')[0]
	ui.lineEdit.setText(';'.join(fileNames))

def send(ui):
	global fileNames
	#压缩
	if os.path.exists('Cache/send_cache.7z'):
			os.remove('Cache/send_cache.7z')
	with py7zr.SevenZipFile('Cache/send_cache.7z','w') as f:
		for ff in fileNames:
			f.writeall(ff)

	towho = ui.lineEdit_2.text()
	sar.sendFile('Cache/send_cache.7z',towho)
	ui.lineEdit.setText('')

def recv(ui):
	if sar.check():
		savePath = QFileDialog.getExistingDirectory()
		sar.recvFile(savePath+'/FTF.7z')
	else:
		QMessageBox.information(None, 'info', 'no file to recv',QMessageBox.Ok)


if __name__ == '__main__':
	app = QApplication(sys.argv)
	MainWindow = QMainWindow()
	ui = gui.Ui_MainWindow()
	ui.setupUi(MainWindow)
	if sar.user:
		ui.label_2.setText(sar.user)
		ui.pushButton_3.setText('change user')
	else:
		ui.label_2.setText('Please login')

	ui.pushButton.clicked.connect(lambda:send(ui))
	ui.pushButton_2.clicked.connect(lambda:recv(ui))
	ui.pushButton_3.clicked.connect(lambda:login(ui))
	ui.toolButton.clicked.connect(lambda:chooseFile(ui))

	MainWindow.show()
	sys.exit(app.exec_())







# with py7zr.SevenZipFile("test.7z","w") as a:
# 	a.writeall("test.csv")

# with py7zr.SevenZipFile("test.7z", 'r') as archive:
#     archive.extractall(path="test/")









